package com.luck.easyfoodbatch.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.entity.Categorys;
import com.luck.easyfoodbatch.mvp.home.CategoryListModel;
import com.luck.easyfoodbatch.mvp.home.CategoryListPresenter;
import com.luck.easyfoodbatch.mvp.home.CategoryListView;
import com.luck.easyfoodbatch.util.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/3.
 */

public class CategoryFragmentCopy extends BaseFragment implements CategoryListView {
    private View rootView;
    private String toolsList[];  //放置标题的数组
    private TextView toolsTextViews[];  //用来显示标题的容器
    private View views[];
    private ImageView iv_back;
    private ViewPager vp_categroy_item;
    private ScrollView sl_categroy;
    private int scrllViewWidth = 0, scrollViewMiddle = 0;
    private int currentItem=0;
    private MyAdapter adapter;
    private CategoryListModel listModel;
//    private ArrayList<CategoryBean.DataBean.CategoryItem> jiulei = new ArrayList<>();
//    private ArrayList<CategoryBean.DataBean.CategoryItem> liangyou = new ArrayList<>();
//    private ArrayList<CategoryBean.DataBean.CategoryItem> lingshi = new ArrayList<>();
//    private ArrayList<CategoryBean.DataBean.CategoryItem> muying = new ArrayList<>();
//    private ArrayList<CategoryBean.DataBean.CategoryItem> rupin = new ArrayList<>();
//    private ArrayList<CategoryBean.DataBean.CategoryItem> chongyin = new ArrayList<>();
//    private ArrayList<CategoryBean.DataBean.CategoryItem> shuilei = new ArrayList<>();
//    private ArrayList<CategoryBean.DataBean.CategoryItem> yinliao = new ArrayList<>();
//    public CategoryFragmentCopy() {
//    }

    private List<Categorys.DataBean> categoryList=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.categroy_copy, container, false);
            View mStateBarFixer = rootView.findViewById(R.id.status_bar_fix);
            mStateBarFixer.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight(getActivity())));
            mStateBarFixer.setBackgroundColor(getResources().getColor(R.color.black));
            showToolsView(rootView);
            listModel = new CategoryListPresenter(this, mContext);
            showProgressDialog("拼命加载中,请稍后");
            listModel.getCategoryList();
            initViews(rootView);
        }
        return rootView;
    }
    private void showToolsView(View view) {
        toolsList=new String[]{"酒类","水类","饮料","乳品","零食","粮油","冲饮","母婴"};
        LinearLayout toolsLayout=(LinearLayout)view.findViewById(R.id.tools);
        toolsTextViews=new TextView[toolsList.length];
        views=new View[toolsList.length];
        for (int i = 0; i < toolsList.length; i++) {
            view=View.inflate(getContext(),R.layout.item_top_nav,null);
            view.setId(i);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    vp_categroy_item.setCurrentItem(v.getId());
                }
            });
            TextView category_name=(TextView) view.findViewById(R.id.category_name);
            category_name.setText(toolsList[i]);
            toolsLayout.addView(view);
            toolsTextViews[i]=category_name;
            views[i]=view;
        }
        changeTextColor(0);
    }

    private void initViews(View view) {
        iv_back= (ImageView) view.findViewById(R.id.iv_back);
        sl_categroy= (ScrollView) view.findViewById(R.id.sv_category_item);
        vp_categroy_item= (ViewPager) view.findViewById(R.id.vp_goods);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.getInstance().exitbefore();

            }
        });
        vp_categroy_item.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //监听ViewPager选项卡变化事的事件
            @Override
            public void onPageSelected(int position) {
                if(vp_categroy_item.getCurrentItem()!=position){
                    vp_categroy_item.setCurrentItem(position);
                }
                if(currentItem!=position){
                    changeTextColor(position);
                    changeTextLocation(position);
                }
                currentItem=position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    /**
     * 改变textView的颜色
     * @param id
     */
    private void changeTextColor(int id) {
        for (int i = 0; i < toolsTextViews.length; i++) {
            if(i!=id){
                toolsTextViews[i].setBackgroundResource(android.R.color.transparent);
                toolsTextViews[i].setTextColor(0xff000000);
            }
        }
        toolsTextViews[id].setBackgroundResource(android.R.color.white);
        toolsTextViews[id].setTextColor(getResources().getColor(R.color.tab_color_true));
    }
    /**
     * 改变栏目位置
     *
     * @param clickPosition
     */
    private void changeTextLocation(int clickPosition) {

        int y = (views[clickPosition].getTop() - getScrollViewMiddle() + (getViewHeight(views[clickPosition]) / 2));
        sl_categroy.smoothScrollTo(0, y);
    }

    /**
     * 返回scrollview的中间位置
     *
     * @return
     */
    private int getScrollViewMiddle() {
        if (scrollViewMiddle == 0)
            scrollViewMiddle = getScrollViewHeight() / 2;
        return scrollViewMiddle;
    }

    /**
     * 返回ScrollView的宽度
     *
     * @return
     */
    private int getScrollViewHeight() {
        if (scrllViewWidth == 0)
            scrllViewWidth = sl_categroy.getBottom() - sl_categroy.getTop();
        return scrllViewWidth;
    }
    /**
     * 返回view的宽度
     *
     * @param view
     * @return
     */
    private int getViewHeight(View view) {
        return view.getBottom() - view.getTop();
    }

    @Override
    public void getCategoryListsSuccess(Categorys model) {
        if (model!=null){
            if (model.getStatus()== HttpUtil.RESULT_OK){
                categoryList= model.getData();
                if (categoryList!=null){

//                    jiulei=data.getJiulei();
//                    liangyou = data.getLiangyou();
//                    lingshi = data.getLingshi();
//                    muying = data.getMuying();
//                    rupin = data.getRupin();
//                    chongyin = data.getChongyin();
//                    shuilei = data.getShuilei();
//                    yinliao = data.getYinliao();
                    adapter = new MyAdapter(getFragmentManager());
                    vp_categroy_item.setAdapter(adapter);
                }
            }
        }

    }

    @Override
    public void httpStatusError() {
        showToast("网络连接不畅,请检查网络!");
    }

    @Override
    public void hideLoading() {
            hideShowProgress();
    }

    private class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment=new CategoryShopFragment();
            Bundle bundle=new Bundle();
            String str=toolsList[position];
//            bundle.putString("typeName",str);
//            bundle.putParcelableArrayList("categoryList",getList(position));
            bundle.putSerializable("categoryBean",categoryList.get(position));
            fragment.setArguments(bundle);
            return fragment;
        }

        @Override
        public int getCount() {
            return toolsList.length;
        }
    }
//    private ArrayList<? extends Parcelable> getList(int position){
//        if (position == 0) {//酒类
//            return jiulei;
//        } else if (position == 1) {//水类
//            return shuilei;
//        } else if (position == 2) {//饮料
//            return yinliao;
//        } else if (position == 3) {//乳品
//            return rupin;
//        } else if (position == 4) {//零食
//            return lingshi;
//        } else if (position == 5) {//粮油
//            return liangyou;
//        } else if (position == 6) {//冲饮
//            return chongyin;
//        } else if (position == 7) {//母婴
//            return muying;
//        }
//        return jiulei;
//    }



//    private ArrayList<CategoryBean.DataBean.CategoryItem> getList(int position){
//        if (position == 0) {//酒类
//            return jiulei;
//        } else if (position == 1) {//水类
//            return shuilei;
//        } else if (position == 2) {//饮料
//            return yinliao;
//        } else if (position == 3) {//乳品
//            return rupin;
//        } else if (position == 4) {//零食
//            return lingshi;
//        } else if (position == 5) {//粮油
//            return liangyou;
//        } else if (position == 6) {//冲饮
//            return chongyin;
//        } else if (position == 7) {//母婴
//            return muying;
//        }
//        return jiulei;
//    }
}
